<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>Mersenne Twister (MT19937) &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../../about.html" >
    <link rel="index" title="Index" href="../../../genindex.html" >
    <link rel="search" title="Search" href="../../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../../index.html" >
    <link rel="up" title="Bit Generators" href="index.html" >
    <link rel="next" title="numpy.random.MT19937.state" href="generated/numpy.random.MT19937.state.html" >
    <link rel="prev" title="numpy.random.BitGenerator.random_raw" href="generated/numpy.random.BitGenerator.random_raw.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../../index.html">
      <img border=0 alt="NumPy" src="../../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../../routines.html" >Routines</a></li>
          <li class="active"><a href="../index.html" >Random sampling (<code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.random</span></code>)</a></li>
          <li class="active"><a href="index.html" accesskey="U">Bit Generators</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="generated/numpy.random.MT19937.state.html" title="numpy.random.MT19937.state"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="generated/numpy.random.BitGenerator.random_raw.html" title="numpy.random.BitGenerator.random_raw"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Mersenne Twister (MT19937)</a><ul>
<li><a class="reference internal" href="#state">State</a></li>
<li><a class="reference internal" href="#parallel-generation">Parallel generation</a></li>
<li><a class="reference internal" href="#extending">Extending</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="generated/numpy.random.BitGenerator.random_raw.html"
                        title="previous chapter">numpy.random.BitGenerator.random_raw</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="generated/numpy.random.MT19937.state.html"
                        title="next chapter">numpy.random.MT19937.state</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="mersenne-twister-mt19937">
<h1>Mersenne Twister (MT19937)<a class="headerlink" href="#mersenne-twister-mt19937" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="numpy.random.MT19937">
<em class="property">class </em><code class="sig-prename descclassname">numpy.random.</code><code class="sig-name descname">MT19937</code><span class="sig-paren">(</span><em class="sig-param">seed=None</em><span class="sig-paren">)</span><a class="headerlink" href="#numpy.random.MT19937" title="Permalink to this definition">¶</a></dt>
<dd><p>Container for the Mersenne Twister pseudo-random number generator.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>seed</strong><span class="classifier">{None, int, array_like[ints], SeedSequence}, optional</span></dt><dd><p>A seed to initialize the <a class="reference internal" href="generated/numpy.random.BitGenerator.html#numpy.random.BitGenerator" title="numpy.random.BitGenerator"><code class="xref py py-obj docutils literal notranslate"><span class="pre">BitGenerator</span></code></a>. If None, then fresh,
unpredictable entropy will be pulled from the OS. If an <code class="docutils literal notranslate"><span class="pre">int</span></code> or
<code class="docutils literal notranslate"><span class="pre">array_like[ints]</span></code> is passed, then it will be passed to
<a class="reference internal" href="generated/numpy.random.SeedSequence.html#numpy.random.SeedSequence" title="numpy.random.SeedSequence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence</span></code></a> to derive the initial <a class="reference internal" href="generated/numpy.random.BitGenerator.html#numpy.random.BitGenerator" title="numpy.random.BitGenerator"><code class="xref py py-obj docutils literal notranslate"><span class="pre">BitGenerator</span></code></a> state. One may also
pass in a <a class="reference internal" href="generated/numpy.random.SeedSequence.html#numpy.random.SeedSequence" title="numpy.random.SeedSequence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence</span></code></a> instance.</p>
</dd>
</dl>
</dd>
</dl>
<p class="rubric">Notes</p>
<p><code class="docutils literal notranslate"><span class="pre">MT19937</span></code> provides a capsule containing function pointers that produce
doubles, and unsigned 32 and 64- bit integers <a class="reference internal" href="#r312276d80bfa-1" id="id1">[1]</a>. These are not
directly consumable in Python and must be consumed by a <code class="docutils literal notranslate"><span class="pre">Generator</span></code>
or similar object that supports low-level access.</p>
<p>The Python stdlib module “random” also contains a Mersenne Twister
pseudo-random number generator.</p>
<p><strong>State and Seeding</strong></p>
<p>The <code class="docutils literal notranslate"><span class="pre">MT19937</span></code> state vector consists of a 624-element array of
32-bit unsigned integers plus a single integer value between 0 and 624
that indexes the current position within the main array.</p>
<p>The input seed is processed by <a class="reference internal" href="generated/numpy.random.SeedSequence.html#numpy.random.SeedSequence" title="numpy.random.SeedSequence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence</span></code></a> to fill the whole state. The
first element is reset such that only its most significant bit is set.</p>
<p><strong>Parallel Features</strong></p>
<p>The preferred way to use a BitGenerator in parallel applications is to use
the <a class="reference internal" href="generated/numpy.random.SeedSequence.spawn.html#numpy.random.SeedSequence.spawn" title="numpy.random.SeedSequence.spawn"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence.spawn</span></code></a> method to obtain entropy values, and to use these
to generate new BitGenerators:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">numpy.random</span> <span class="kn">import</span> <span class="n">Generator</span><span class="p">,</span> <span class="n">MT19937</span><span class="p">,</span> <span class="n">SeedSequence</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sg</span> <span class="o">=</span> <span class="n">SeedSequence</span><span class="p">(</span><span class="mi">1234</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rg</span> <span class="o">=</span> <span class="p">[</span><span class="n">Generator</span><span class="p">(</span><span class="n">MT19937</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">sg</span><span class="o">.</span><span class="n">spawn</span><span class="p">(</span><span class="mi">10</span><span class="p">)]</span>
</pre></div>
</div>
<p>Another method is to use <a class="reference internal" href="generated/numpy.random.MT19937.jumped.html#numpy.random.MT19937.jumped" title="numpy.random.MT19937.jumped"><code class="xref py py-obj docutils literal notranslate"><span class="pre">MT19937.jumped</span></code></a> which advances the state as-if
<img class="math" src="../../../_images/math/91018ad7005c40f683c645b99dd28ae741d2de00.svg" alt="2^{128}"/> random numbers have been generated (<a class="reference internal" href="#r312276d80bfa-1" id="id2">[1]</a>, <a class="reference internal" href="#r312276d80bfa-2" id="id3">[2]</a>). This
allows the original sequence to be split so that distinct segments can be
used in each worker process. All generators should be chained to ensure
that the segments come from the same sequence.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">numpy.random</span> <span class="kn">import</span> <span class="n">Generator</span><span class="p">,</span> <span class="n">MT19937</span><span class="p">,</span> <span class="n">SeedSequence</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sg</span> <span class="o">=</span> <span class="n">SeedSequence</span><span class="p">(</span><span class="mi">1234</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">bit_generator</span> <span class="o">=</span> <span class="n">MT19937</span><span class="p">(</span><span class="n">sg</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rg</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
<span class="gp">... </span>   <span class="n">rg</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Generator</span><span class="p">(</span><span class="n">bit_generator</span><span class="p">))</span>
<span class="gp">... </span>   <span class="c1"># Chain the BitGenerators</span>
<span class="gp">... </span>   <span class="n">bit_generator</span> <span class="o">=</span> <span class="n">bit_generator</span><span class="o">.</span><span class="n">jumped</span><span class="p">()</span>
</pre></div>
</div>
<p><strong>Compatibility Guarantee</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">MT19937</span></code> makes a guarantee that a fixed seed and will always produce
the same random integer stream.</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r312276d80bfa-1"><span class="brackets">1</span><span class="fn-backref">(<a href="#id1">1</a>,<a href="#id2">2</a>)</span></dt>
<dd><p>Hiroshi Haramoto, Makoto Matsumoto, and Pierre L’Ecuyer, “A Fast
Jump Ahead Algorithm for Linear Recurrences in a Polynomial Space”,
Sequences and Their Applications - SETA, 290–298, 2008.</p>
</dd>
<dt class="label" id="r312276d80bfa-2"><span class="brackets"><a class="fn-backref" href="#id3">2</a></span></dt>
<dd><p>Hiroshi Haramoto, Makoto Matsumoto, Takuji Nishimura, François
Panneton, Pierre L’Ecuyer, “Efficient Jump Ahead for F2-Linear
Random Number Generators”, INFORMS JOURNAL ON COMPUTING, Vol. 20,
No. 3, Summer 2008, pp. 385-390.</p>
</dd>
</dl>
<dl class="field-list simple">
<dt class="field-odd">Attributes</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>lock: threading.Lock</strong></dt><dd><p>Lock instance that is shared so that the same bit git generator can
be used in multiple Generators without corrupting the state. Code that
generates values from a bit generator should hold the bit generator’s
lock.</p>
</dd>
</dl>
</dd>
</dl>
</dd></dl>

<div class="section" id="state">
<h2>State<a class="headerlink" href="#state" title="Permalink to this headline">¶</a></h2>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.random.MT19937.state.html#numpy.random.MT19937.state" title="numpy.random.MT19937.state"><code class="xref py py-obj docutils literal notranslate"><span class="pre">state</span></code></a></p></td>
<td><p>Get or set the PRNG state</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="parallel-generation">
<h2>Parallel generation<a class="headerlink" href="#parallel-generation" title="Permalink to this headline">¶</a></h2>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.random.MT19937.jumped.html#numpy.random.MT19937.jumped" title="numpy.random.MT19937.jumped"><code class="xref py py-obj docutils literal notranslate"><span class="pre">jumped</span></code></a>([jumps])</p></td>
<td><p>Returns a new bit generator with the state jumped</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="extending">
<h2>Extending<a class="headerlink" href="#extending" title="Permalink to this headline">¶</a></h2>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.random.MT19937.cffi.html#numpy.random.MT19937.cffi" title="numpy.random.MT19937.cffi"><code class="xref py py-obj docutils literal notranslate"><span class="pre">cffi</span></code></a></p></td>
<td><p>CFFI interface</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.random.MT19937.ctypes.html#numpy.random.MT19937.ctypes" title="numpy.random.MT19937.ctypes"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ctypes</span></code></a></p></td>
<td><p>ctypes interface</p></td>
</tr>
</tbody>
</table>
</div>
</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>